﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using Common.Contract.Authentication.Record;
using Common.Service;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Request;
using Common.Contract;
using SMECustomerWebsite.Core.Configuration;

namespace SMECustomerWebsite.Jobs
{
    public class JobBase
    {
        public bool SendEmail(EmailContent content)
        {
            bool isSuccess = true;
            try
            {
                using (var message = new MailMessage("tudd1@vpbank.com.vn", content.To))
                {
                    message.Subject = content.Subject;
                    message.Body = content.Body;
                    if (content.CC != null && content.CC.Count > 0)
                    {
                        content.CC.ForEach(x =>
                        {
                            message.CC.Add(x);
                        });
                    }
                    using (SmtpClient client = new SmtpClient
                    {
                        EnableSsl = true,
                        Host = "smtp.office365.com",
                        Port = 25,
                        Credentials = new NetworkCredential("tudd1@vpbank.com.vn", "Abc@1234")
                    })
                    {
                        client.Send(message);
                        isSuccess = true;
                    }
                }

            }
            catch (Exception)
            {
                isSuccess = false;
            }
            return isSuccess;
        }


        public void UpdateStatusAfterSendEmail(List<RequestCreateDaoRecord> listRequestNeedSendMail)
        {
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                UserRecord CurrentUserRecord = new UserRecord
                {
                    UserName = "cucnt3",
                    GroupIdentifier = "1"
                };
                UpdateCreateDaoRequestRequest requestUpdate = new UpdateCreateDaoRequestRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.DaoManager.ApproveRequestCreateDao"
                    },
                    //Record = userRequest,
                    CurrentUser = CurrentUserRecord,
                    ListCreateDaoRequest = listRequestNeedSendMail
                };
                var result = serviceClient.Proxy.UpdateStatusListRequestDao(requestUpdate);
            }
        }
        public class EmailContent
        {
            public string Subject { get; set; }
            public string Body { get; set; }
            public string To { get; set; }
            public List<string> CC { get; set; }
        }
    }
}